package com.solotiger.geographic.common;

import com.jakewharton.rxbinding2.view.RxView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;

import static com.solotiger.geographic.common.GlobalConstants.GLOBAL_DOWNLOAD_PATH;

public enum AppEngine {
    ME;

    private List<String> downloadList;

    public void init() {
        downloadList = new ArrayList<>();
        File fileDr = new File(GLOBAL_DOWNLOAD_PATH);
        if (!fileDr.exists()) {
            fileDr.mkdir();

            return;
        }
        Observable.just(fileDr)
                .flatMap(new Function<File, Observable<File>>() {
                    @Override
                    public Observable<File> apply(File file) {
                        return Observable.fromArray(file.listFiles());
                    }
                })
                .filter(new Predicate<File>() {
                    @Override
                    public boolean test(File file) {
                        String end = file.getName().toLowerCase();
                        return end.endsWith(".jpg") || end.endsWith(".png") || end.endsWith(".jepg");
                    }
                })
                .map(new Function<File, String>() {
                    @Override
                    public String apply(File file) {
                        return file.getName();
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.newThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String fileName) {
                        downloadList.add(fileName);
                    }
                });

    }

    public List<String> getDownloadList() {
        return downloadList;
    }

    public void setDownloadList(List<String> downloadList) {
        this.downloadList = downloadList;
    }
}
